package com.lut.teacher.teacherservice.service.impl;

import com.lut.teacher.teacherservice.entity.TeacherInfoEntity;
import com.lut.teacher.teacherservice.entity.TeacherTitleEntity;
import com.lut.teacher.teacherservice.entity.vo.TeacherProjectVo;
import com.lut.teacher.teacherservice.entity.vo.TeacherTitleVo;
import com.lut.teacher.teacherservice.service.TeacherInfoService;
import com.lut.teacher.teacherservice.utils.PageUtils;
import com.lut.teacher.teacherservice.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.lut.teacher.teacherservice.dao.TeacherProjectDao;
import com.lut.teacher.teacherservice.entity.TeacherProjectEntity;
import com.lut.teacher.teacherservice.service.TeacherProjectService;
import org.springframework.util.StringUtils;


@Service("teacherProjectService")
public class TeacherProjectServiceImpl extends ServiceImpl<TeacherProjectDao, TeacherProjectEntity> implements TeacherProjectService {
   @Autowired
   private TeacherInfoService teacherInfoService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TeacherProjectEntity> page = this.page(
                new Query<TeacherProjectEntity>().getPage(params),
                new QueryWrapper<TeacherProjectEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryProject(Map<String, Object> params) {
        QueryWrapper<TeacherInfoEntity> wrapper1 = new QueryWrapper<>();
        String key= (String) params.get("key");
        QueryWrapper<TeacherProjectEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper1.eq("name", key);
            TeacherInfoEntity one = teacherInfoService.getOne(wrapper1);
            if(!StringUtils.isEmpty(one)){
                wrapper.eq("teacher_id",one.getId());
            }else {
                wrapper.eq("teacher_id","nnul");
                IPage<TeacherProjectEntity> page1 = this.page(
                        new Query<TeacherProjectEntity>().getPage(params),
                        wrapper

                );
                return new PageUtils(page1);
            }

        }

        IPage<TeacherProjectEntity> page = this.page(
                new Query<TeacherProjectEntity>().getPage(params),
                wrapper

        );
        PageUtils pageUtils = new PageUtils(page);
        List<TeacherProjectEntity> records=page.getRecords();

        List<TeacherProjectVo> id1 = records.stream().map(item -> {
            TeacherProjectVo teacherTitleVo = new TeacherProjectVo();
            BeanUtils.copyProperties(item, teacherTitleVo);
            TeacherInfoEntity id = teacherInfoService.getOne(new QueryWrapper<TeacherInfoEntity>().eq("id", item.getTeacherId()));
            if(!StringUtils.isEmpty(id)){
                teacherTitleVo.setTeacherName(id.getName());
            }

            return teacherTitleVo;

        }).collect(Collectors.toList());
        pageUtils.setList(id1);
        return pageUtils;
    }

    @Override
    public List<Map<String, Object>> queryProjectDta() {
        QueryWrapper<TeacherProjectEntity>  wrapper=new QueryWrapper<>();
        wrapper.select(" project_award as projectName,count(*) as total");
        wrapper.groupBy("project_award");
        List<Map<String, Object>> maps = this.listMaps(wrapper);

        return maps;
    }

}